<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\Request;

class Tools extends Base
{
    /**
     * 上传单个文件，包含图片
     * @param Request $request
     * @return \think\response\Json
     */
    public function uploadpic(Request $request)
    {
        try {
            $file = $request->file('file');
            $filename = $file->getOriginalName();
            $savename = \think\facade\Filesystem::disk('public')->putFile( 'addproject', $file);
            $url = config('filesystem.disks.public.url');
            if($savename){
                $arr = str_replace('\\','/',$savename);
                $result = ["status"=>true,"path"=>$url."/".$arr,"filename"=>$filename,"msg"=>"上传图片成功！"];
            }else{
                $result = ["status"=>false,"path"=>"","msg"=>"上传图片失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 多个文件上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function uploadpicmore(Request $request)
    {
        try {
            $files = $request->file('file');
            //if($files){
                $savename = [];
                foreach($files as $file) {
                    $savename[] = \think\facade\Filesystem::disk('public')->putFile('addprojectmore', $file);
                }
                $url = config('filesystem.disks.public.url');
                if($savename){
                    $arr = [];
                    foreach ($savename as $v){
                        $arr[] = $url."/".str_replace('\\','/',$v);
                    }
                    $result = ["status"=>true,"path"=>$arr,"msg"=>"上传图片成功！"];
                }else{
                    $result = ["status"=>false,"path"=>"","msg"=>"上传图片失败！"];
                }
                return json($result);
            //}else{
                //return $this->fetch();
            //}

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 推送（esb）
     * @param Request $request
     * @return \think\response\Json
     */
    public function sendEsbMsg(Request $request)
    {
        try {


        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
